
package cn.com.pusai.core.util;

import com.sun.crypto.provider.SunJCE;
import java.io.PrintStream;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

/**
 * DES加密解密
 * */
public class DesUtil {

	
	private final static String KEY="原始数据:我要加密";
	public DesUtil() {
	}

	

	/** 
	 * 加密方法 DES
	 * @param 要加密的数据
	 * @param 密钥  最少是8个字节 一个中文两个字节
	 * @return 解密后的数据
	 * */
	public static String encry(String data, String privateKey) {
		String algorithm;
		Security.addProvider(new SunJCE());
		algorithm = "DES";
		byte cipherByte[];
		try {
			DESKeySpec desKeySpec = new DESKeySpec(privateKey.getBytes());
			SecretKeyFactory keyFactory = SecretKeyFactory
					.getInstance(algorithm);
			javax.crypto.SecretKey deskey = keyFactory
					.generateSecret(desKeySpec);
			Cipher c1 = Cipher.getInstance(algorithm);
			c1.init(1, deskey);
			cipherByte = c1.doFinal(data.getBytes());
			return byteToHex(cipherByte);
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return null;
	}
	
	
	/**
	 * 加密方法 DES
	 * @param 要加密的数据
	 * @return 解密后的数据
	 * */
	public static String encry(String data) {
		String algorithm;
		Security.addProvider(new SunJCE());
		algorithm = "DES";
		byte cipherByte[];
		try {
			KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
			javax.crypto.SecretKey deskey = keygen.generateKey();
			Cipher c1 = Cipher.getInstance(algorithm);
			c1.init(1, deskey);
			cipherByte = c1.doFinal(data.getBytes());
			return byteToHex(cipherByte);
		} catch (NoSuchAlgorithmException e1) {

			e1.printStackTrace();
		} catch (NoSuchPaddingException e2) {
			e2.printStackTrace();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 解密方法 DES
	 * @param 加密后的数据
	 * @param 加密的密钥  最少是8个字节 一个中文两个字节
	 * @return 解密后的数据
	 * */
	public static String decry(String hexData, String privateKey) {
		String algorithm;
		Security.addProvider(new SunJCE());
		algorithm = "DES";
		byte clearByte[];
		try {
			DESKeySpec desKeySpec = new DESKeySpec(privateKey.getBytes());
			SecretKeyFactory keyFactory = SecretKeyFactory
					.getInstance(algorithm);
			javax.crypto.SecretKey deskey = keyFactory
					.generateSecret(desKeySpec);
			Cipher c1 = Cipher.getInstance(algorithm);
			c1.init(2, deskey);
			clearByte = c1.doFinal(hexToByte(hexData));
			return new String(clearByte);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 解密方法 DES
	 * @param 加密后的数据
	 * @return 解密后的数据
	 * */
	public static String decry(String hexData) {
		String algorithm;
		Security.addProvider(new SunJCE());
		algorithm = "DES";
		byte clearByte[];
		try {
			KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
			javax.crypto.SecretKey deskey = keygen.generateKey();
			Cipher c1 = Cipher.getInstance(algorithm);
			c1.init(2, deskey);
			clearByte = c1.doFinal(hexToByte(hexData));
			return new String(clearByte);
		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return null;

	}

	private static String byteToHex(byte data[]) {
		StringBuilder sb = new StringBuilder(data.length * 2);
		for (int i = 0; i < data.length; i++) {
			sb.append(Integer.toHexString((0xf0 & data[i]) >>> 4));
			sb.append(Integer.toHexString(0xf & data[i]));
		}

		return new String(sb);
	}

	private static byte[] hexToByte(String hexData) {
		byte data[] = new byte[hexData.length() / 2];
		for (int i = 0; i < data.length; i++)
			data[i] = (byte) Integer.parseInt(hexData.substring(2 * i,
					2 * i + 2), 16);

		return data;
	}
	
	public static void main(String args[]) {
		String data = "1382648561011123asd";
		System.out.println((new StringBuilder("原始数据： ")).append(data)
				.toString());
		String encryData = encry(data, "原始数aaa数");
		System.out.println((new StringBuilder("加密后数据： ")).append(encryData)
				.toString());
		String decryData = decry(encryData, "原始数aaa数");
		System.out.println((new StringBuilder("解密后数据： ")).append(decryData)
				.toString());
	}
}
